﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL.DBHelpers;
namespace QuanLyKhachHang
{
    public partial class uc_LoaiSP : UserControl
    {
        public uc_LoaiSP()
        {
            InitializeComponent();
        }
        public void resetfield()
        {
            txtLoaiSP.Text = "";
            cbTrangThai.Checked = false;
        }
        public void LoadData()
        {
            var dt = DBHelper.GetDataTableSP("sp_LoaiSP_getAllLoaiSP");
            gdLoaiSP.DataSource = dt;
        }
        private void uc_LoaiSP_Load(object sender, EventArgs e)
        {
            LoadData();
            
        }
        string Id = string.Empty;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            string b = string.Empty;
            if (DBHelper.CheckExitInDB(txtLoaiSP.Text, "LoaiSP", "sp_LoaiSP_getAllLoaiSP") == true)
            {
                MessageBox.Show("Loại sản phẩm đã tồn tại !");
                return;
            }
            else b = txtLoaiSP.Text;
            ls.Add(new SqlParameter("@LoaiSP",b));
            Boolean a ;
            if (cbTrangThai.Checked) a = true;
            else a = false;
            ls.Add(new SqlParameter("@TrangThai",a));
            DBHelper.ExecuteNonQuery("sp_LoaiSP_Add", ls);
            LoadData();
            MessageBox.Show("Thêm thành công !");
            resetfield();
            
        }

        private void BtnExport_Click(object sender, EventArgs e)
        {
            gdLoaiSP.ShowPrintPreview();
        }

        private void BtnEdit_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@LoaiSP", txtLoaiSP.Text));
            Boolean a;
            if (cbTrangThai.Checked) a = true;
            else a = false;
            ls.Add(new SqlParameter("@TrangThai", a));
            DBHelper.ExecuteNonQuery("sp_LoaiSP_Update", ls);
            LoadData();
            resetfield();
            MessageBox.Show("Cập nhật thành công !");
        }

        private void gdLoaiSP_Click(object sender, EventArgs e)
        {
            Id = gdvNV.GetFocusedDisplayText();
            txtLoaiSP.Text = gdvNV.GetFocusedRowCellDisplayText(col_SP);
            string a = gdvNV.GetFocusedRowCellDisplayText(col_TrangThai);
            if (a == "Checked") cbTrangThai.Checked = true;
            else cbTrangThai.Checked = false;
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            DBHelper.ExecuteNonQuery("sp_LoaiSP_Delete",ls);
            LoadData();
            resetfield();
            MessageBox.Show("Xóa thành công !");
        }
    }
}
